package manage.controller;

import manage.dao.UserMapper;
import manage.model.File;
import manage.model.User;
import manage.service.impl.FileServiceImpl;
import manage.util.LogUtil;
import manage.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class FileController {
    @Autowired
    private FileServiceImpl fileService;

    @Autowired
    private UserMapper userMapper;

    private User user;
    //返回File的list
    @RequestMapping(value = "/file")
    @ResponseBody
    public Msg FileData(HttpServletRequest request, Authentication authentication){
        String page = request.getParameter("currentPage");
        String limit = request.getParameter("pageSize");
        UserDetails userDetails =(UserDetails) authentication.getPrincipal();
        user = userMapper.selectByPrimaryKey(userDetails.getUsername());
        return Msg.success().add("data",fileService.selectAll(page,limit));
    }

    @RequestMapping(value = "/file1")
    @ResponseBody
    public Msg File1Data(HttpServletRequest request, Authentication authentication){
        String page = request.getParameter("currentPage");
        String limit = request.getParameter("pageSize");
        UserDetails userDetails =(UserDetails) authentication.getPrincipal();
        user = userMapper.selectByPrimaryKey(userDetails.getUsername());
        return Msg.success().add("data",fileService.selectAll1(page,limit));
    }

    //添加File
    @ResponseBody
    @RequestMapping(value = "/addFile",method = RequestMethod.POST)
    public Msg addFile(File file){
        file.setOperatorDate(new Date());
        file.setOperatorId(user.getAccount());
        file.setSubmit("否");
        String s= fileService.insert(file);
        LogUtil.writeFileLog(user.getAccount(),"添加文件类型:"+file.toString());
        return Msg.attendanceAdd(s);
    }

    //修改File
    @ResponseBody
    @RequestMapping(value = "/addFile",method = RequestMethod.PUT)
    public Msg updateFile(File file){
        file.setOperatorDate(new Date());
        file.setOperatorId(user.getAccount());
        String s= fileService.update(file);
        LogUtil.writeFileLog(user.getAccount(),"修改文件类型-改:"+file.toString());
        return Msg.attendanceAdd(s);
    }
    //删除File
    @ResponseBody
    @RequestMapping("/delFile")
    public Msg delFile(@RequestBody Integer id){
        String s = fileService.del(id);
        LogUtil.writeFileLog(user.getAccount(),"删除文件类型:"+id.toString());
        return Msg.attendanceAdd(s);
    }

    //根据主键查File 和修改配合使用
    @ResponseBody
    @RequestMapping("/fileSelect")
    public Msg selFile(@RequestBody Integer id){
        File file = fileService.select(id);
        LogUtil.writeFileLog(user.getAccount(),"修改文件类型-查:"+file.toString());
        return Msg.success().add("file",file);
    }
    @ResponseBody
    @RequestMapping("/fileDown")
    public Msg setDown(@RequestBody Integer id){
        File file = fileService.select(id);
        file.setSubmit("是");
        String s= fileService.update(file);
        LogUtil.writeFileLog(user.getAccount(),"修改文件类型-查:"+file.toString());
        return Msg.success();
    }
}
